package com.azxcrm.project.system.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.azxcrm.framework.redis.template.service.RedisServiceImpl;
import com.azxcrm.framework.redis.vo.RedisVo;

@Controller
@RequestMapping("/redis")
public class RedisController {
	
	@Autowired
	private RedisServiceImpl redisServiceImpl;
	
	@GetMapping("addStr")
	@ResponseBody
	public String addRedisKV(){
		boolean flag = false;
		try {
			flag = redisServiceImpl.set("RedisKey", "RedisValue");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "存储K-V状况: "+flag;
	}
	
	@GetMapping("addObj")
	@ResponseBody
	public void addRedisObject(){
		JSONObject obj = new JSONObject();
		try {
			RedisVo vo = new RedisVo("Redis",185);
			redisServiceImpl.setObject("RedisVO1", vo);
			
			obj.put("vo", (JSON) JSONObject.toJSON(redisServiceImpl.get("RedisVO1")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		JSONUtils.toJSONString(obj);
	}
	
	@GetMapping("/getObj/{key}")
	@ResponseBody
	public Object getObjectBuKey(@PathVariable("key") String key){
		Object result = null;
		try {
			result  = redisServiceImpl.get(key);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
